//
//  ONLY.m
//  ProjectA
//
//  Created by lanouhn on 15/5/28.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "ONLY.h"
#import "FirstModel.h"

@interface ONLY ()


@end
@implementation ONLY
- (void)dealloc {
    [_array release];
    [super dealloc];
}
- (NSString *)onlyPath{
    if (!_onlyPath) {
        NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *str = [Path stringByAppendingString:@"/array.plist"];
        self.onlyPath = [NSString stringWithFormat:@"%@", str];
    }
    return _onlyPath;
 
}
//创建单例
+ (id)creatOnly{
    static ONLY *only = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        only = [[self alloc]init];
  
    });
    return only;
}
- (NSMutableArray *)array{
    if (!_array) {
        
        self.array =[NSKeyedUnarchiver unarchiveObjectWithFile:[[ONLY creatOnly] onlyPath]];
        if (_array == nil) {
            self.array = [NSMutableArray array];
        }
    }
    return  _array;
}

- (void)passingValueWithContentmodel:(FirstModel *)model{
    for (FirstModel *modelIn in self.array) {
        if ([modelIn.title isEqual: model.title]) {
            [self.array removeObject:modelIn];
            [self.array insertObject:model atIndex:0];
            return;
        }
    }
//    [self.array addObject:model];
    [self.array insertObject:model atIndex:0];
    [NSKeyedArchiver archiveRootObject:self.array toFile:self.onlyPath];
    }
@end
